<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>        

<script type="text/javascript">	
	$(document).ready(function(){
		//마우스 위치 저장할 변수
//		var x = null; 
//		var y = null; 
		//초기에 화면에 뿌려줄 내용
		$("#total_gugunDiv").hide();
		$.ajax({
			type:"POST",
			url:"hopeSubjectList.do",
			dataType:"json",
			success:function(jsonData){	
				//시도 리스트 가져와서 뿌리기
				$("#total_sido option").remove();
				var op="<option value='false'>--</option>";
				$("#total_sido").append(op);
 				$.each(jsonData.sidoList,function(index, sidolist){	//jsonData.sidoList을 sidolist란 이름으로 사용
 					op="<option value="+sidolist.code_code+">"+sidolist.code_name+"</option>";
					$("#total_sido").append(op); 
				});

 				//희망과목 가져와서 뿌리기
				$("#total_hopesubject option").remove();
				op="<option value=''>--</option>";
				$("#total_hopesubject").append(op);
 				$.each(jsonData.hopeSubjectList,function(index,hopesubjectlist){
 					op="<option value="+hopesubjectlist.code_code+">"+hopesubjectlist.code_name+"</option>";
					$("#total_hopesubject").append(op); 
				});
 				//성별 가져와서 뿌리기
				$("#total_gender option").remove();
				op="<option value=''>--</option>";
				$("#total_gender").append(op);
 				$.each(jsonData.genderList,function(index, genderlist){
 					op="<option value="+genderlist.code_code+">"+genderlist.code_name+"</option>";
					$("#total_gender").append(op); 
				});
 				//학교학년 가져와서 뿌리기
				$("#total_grade option").remove();
				op="<option value=''>--</option>";
				$("#total_grade").append(op);
 				$.each(jsonData.gradeList,function(index, gradelist){
 					op="<option value="+gradelist.code_code+">"+gradelist.code_name+"</option>";
					$("#total_grade").append(op); 
				});
			}//success
		});//ajax

		$("#total_sido").change(function(){
			total_sido=$(this).val();
			if(total_sido==false){//시도 값이 바뀌었는데 --을 선택한 경우
				$("#total_gugunDiv").hide();
			}else{						
				$("#total_gugunDiv").show();				
			}			
			$.ajax({
				type:"POST",
				url:"gugunList.do",
				data:"code_code="+total_sido,
				dataType:"json",
				success:function(jsonData){
	 				//구군정보 가져와서 뿌리기
					$("#total_gugun option").remove();
					var op="<option value=''>--</option>";
					$("#total_gugun").append(op);
	 				$.each(jsonData,function(index, codemaster){
	 					op="<option value="+codemaster.code_code+">"+codemaster.code_name+"</option>";
						$("#total_gugun").append(op); 
					});
				}
			}); 
		});
		
		$("#total_searchBtn").click(function(){
			var total_sido=$("#total_sido").val();		//시도
			var total_gugun=$("#total_gugun").val();//구군
			var total_name=$("#total_name").val();	//선생님이름
			var total_university=$("#total_university").val();	//출신대학
			var total_gender=$("#total_gender").val();		//선생님 성별
			var total_hopesubject=$("#total_hopesubject").val();	//희망과외과목
//			alert("시도:"+total_sido+"구군"+total_gugun+"이름"+total_name+"출신대학"+total_university+"성별"+total_gender+"과목"+total_hopesubject);
			if(total_sido=="false" && (total_gugun==null || total_gugun=="") && total_university=="" && total_gender=="" && total_hopesubject=="" && total_name==""){
				alert("검색조건을 선택하세요.");
			}else{	//조건에 따른 검색
				$.ajax({
					type:"POST",
					url:"getSearchTeacherbyTotal.do",
					data:"hoperegion="+total_gugun+"&name="+total_name+"&university="+total_university+"&gender="+total_gender+"&hopesubject="+total_hopesubject,
					dataType:"json",
					success:function(jsonData){
						var member_table="";
						if(jsonData.list.length>=1){	//검색결과 1명이상이면
							$("#total_totalstudent").text("검색결과 총 선생님 수 : "+jsonData.list.length);
							$("#listbytotal tbody").text("");	//기존의 내용을 지운다.
	 		 				$.each(jsonData.list,function(index, tvo){
	 		 					member_table  = "<tr><td align='center'>"+tvo.name +"</td>"+
																"<td align='center'>"+tvo.hoperegion +"</td>"+
																"<td align='center'>"+tvo.gender +"</td>"+	 		 													
																"<td align='center'>"+tvo.university +"</td>"+
																"<td align='center'>"+tvo.hopesubject +"</td>"+
																"<td align='center'>"+tvo.availablestudent +"</td>"+
																"<td align='center'>"+tvo.availableday +"</td></tr>";
	 							$("#total_memberlist_Tbody").append(member_table);				
							});	 		 				 		 				
							$("#total_sido").val("");	//검색완료 후 초기화(시도)
							$("#total_gugun").val("");		//검색완료 후 초기화(구군)
							$("#total_name").val("");	//검색완료 후 초기화(이름)
							$("#total_university").val("");		//검색완료 후 초기화(출신대학)
							$("#total_gender").val("");	//검색완료 후 초기화(성별)
							$("#total_hopesubject").val("");		//검색완료 후 초기화(희망과외과목)
						}else{	//검색결과가 0명이면
							alert("검색조건에 맞는 선생님이 없습니다.");
						}						
					}
				});			//ajax	
			}	//if
		});	//click
		
		<%--
		 다이얼로그 설정 부분. 로그인한 사람에 따라 다르게 보입니다.
		--%>
		
		 if($("input[name=membertype]").val()=="s"){
			 var dataString = "teacherid="+$("a[id=teacherName]").next().val()
				+"&studentid="+$("input[name=id]").val()
				+"&listtype=s";
			 $( "#dialog_teacherInfo" ).dialog({
			        autoOpen: false,
			        show: { effect: "blind", duration: 100 },
			        hide: {effect: "explode", duration: 100},
			        buttons: {
			        	"찜하기": function(){
			        		$.ajax({
			        			type:"POST",
			    				url:"registerSTSelectedList.do",
			    				data:dataString,  
			    				dataType:"json",
			    				success:function(jsonData){
			    					if(jsonData=="true"){
			    						$( "#dialog_teacherInfo" ).dialog({
			    					        autoOpen: false,
			    					        show: { effect: "blind", duration: 100 },
			    					        hide: {effect: "explode", duration: 100},
			    					        buttons: {
			    					        	"찜리스트 보기": function(){
			    					        		location.href="showSelectedList.do?id="+$("input[name=id]").val()+
			    					        			"&membertype="+$("input[name=membertype]").val();
			    					        	}
			    					        }
			    					 });
			    					}
			    				}
			        		});
			        	}
			        }
//			        position: [x,y+10]
			 });
		 }else{
			 $( "#dialog_teacherInfo" ).dialog({
			        autoOpen: false,
			        show: { effect: "blind", duration: 100 },
			        hide: {effect: "explode", duration: 100}
			 });
		 }
	   
		
		//선생님 이름 클릭되면
 		$("a[id=teacherName]").click(function(e){ 

	
			var teacherId=$(this).next().val();
			var information="";
			$.ajax({
				type:"POST",
				url:"getTeacherInfobyId.do",
				data:"teacherId="+teacherId,  
				dataType:"json",
				success:function(jsonData){

					$("#p_teacherIdInfo").text("");	//기존의 내용을 지운다.
					var string="<img src='${initParam.root }upload/"+jsonData.savefilename+"' width='150'><br>";
					if(jsonData.savefilename==null){
						string="<img src='${initParam.root }img/default.jpg' width='150'><br>"
					}
					information  = string+
										  "<font size='2'>이름 : "+jsonData.name+"</font><br>"+
										  "<font size='2'>과외지역 : "+jsonData.hoperegion+"</font><br>"+
										  "<font size='2'>과외과목 : "+jsonData.hopesubject+"</font><br>"+
										  "<font size='2'>가능학년 : "+jsonData.availablestudent+"</font><br>"+
										  "<font size='2'>전화번호 : "+jsonData.tel+"</font><br>"+
										  "<font size='2'>이메일 : "+jsonData.email+"</font><br>";
										 
										 						  
 					$("#dialog_teacherInfo").html(information);
 					$( "#dialog_teacherInfo").dialog( "open" );
				}	//success
			});	//ajax 			
		});
	});
</script>

<input type="hidden" name="id" value="${sessionScope.mvo.id }">
<input type="hidden" name="membertype" value="${sessionScope.mvo.membertype }">


<form id=total_searchForm>
<table width="560"  cellpadding="2" cellspacing="0"><tr><td>
	<table>
		<tr>
			<td>희망지역</td>
			<td ><select id = "total_sido"></select></td>
			<td colspan="2"> <span id="total_gugunDiv" ><select id="total_gugun"></select></span></td>
			<td>이름</td><td><input type="text" id="total_name"></td>
		</tr>
		<tr>
			<td>출신대학</td><td><input type="text" id="total_university"></td>		
			<td>성별</td><td><select id="total_gender"></select></td>
			<td>희망과목</td><td><select id="total_hopesubject"></select></td>	
			<td align="right"><input type="button" id="total_searchBtn" value="검색"> </td>		
		</tr>
	</table>
</td></tr></table>	
</form>
<table width="560" cellpadding="2" cellspacing="0"><tr align="right"><td>
<div id="total_totalstudent">총 선생님 수 : ${requestScope.listVO.pagingBean.totalContent}</div>
</td></tr></table>	
<table id="listbytotal"  width="560"  border="1" cellpadding="2" cellspacing="0">
	<thead>
		<tr><td width="10%" align="center">이름</td>
			 <td width="20%" align="center">희망지역</td>
			 <td width="8%" align="center">성별</td>
		 	 <td width="15%" align="center">출신학교</td>			
			 <td width="15%" align="center">과외과목</td>
			 <td width="18%" align="center">대상학생</td>
			 <td width="10%" align="center">가능요일</td>		
		</tr>
	</thead>
	<tbody id="total_memberlist_Tbody">
			<c:forEach var="tvo" items="${requestScope.listVO.list}">		
			<tr>
			    <td align='center'><a href="#" id="teacherName">${tvo.name }</a>
			    <input type="hidden" id="hidden_tvo" value="${tvo.id }"></td>
			    <td align="center">${tvo.hoperegion}</td>
			    <td align="center">${tvo.gender}</td>
			    <td align="center">${tvo.university}</td>							
				<td align="center">${tvo.hopesubject}</td>
			    <td align="center">${tvo.availablestudent}</td>							
				<td align="center">${tvo.availableday}</td>								
			</tr>
			</c:forEach>	
	</tbody>	
</table><br>
<div id="dialog_teacherInfo" title="개인정보">
	<p id="p_teacherIdInfo"></p>
</div>
<table width="560"  cellpadding="2" cellspacing="0"><tr align="center"><td>
<c:set var="pb" value="${requestScope.listVO.pagingBean}"></c:set>
<c:if test="${pb.previousPageGroup}">
	<a href="teacherListBasic.do?pageNo=${pb.startPageOfPageGroup-1 }">	Pre </a>
</c:if>	

<c:forEach var="pageNo" begin="${pb.startPageOfPageGroup }" 
										end="${pb.endPageOfPageGroup }">
<c:choose>
<c:when test="${pb.nowPage !=pageNo }" >
	<a href="teacherListBasic.do?pageNo=${pageNo }" >${pageNo }</a>
</c:when>
<c:otherwise>
	${pageNo} 
</c:otherwise>
</c:choose>
</c:forEach>

<c:if test="${pb.nextPageGroup}">
	<a href="teacherListBasic.do?pageNo=${pb.endPageOfPageGroup+1 }">Next </a>
</c:if>
</td></tr></table>	
<br>
